package StudyCode.summary.utf;

import java.awt.*;
import java.io.*;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class PaintStringCharacterByCharacterByCustomFont {
	static void writeOutput(String str) {

		try {
			FileOutputStream fos = new FileOutputStream("E:/Copy/test1.txt");
			Writer out = new OutputStreamWriter(fos, "UTF8");
			out.write(str);
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	static String readInput() {

		StringBuffer buffer = new StringBuffer();
		try {
			FileInputStream fis = new FileInputStream("E:/Copy/test1.txt");
			InputStreamReader isr = new InputStreamReader(fis, "UTF8");
			Reader in = new BufferedReader(isr);
			int ch;
			while ((ch = in.read()) > -1) {
				buffer.append((char) ch);
			}
			in.close();
			return buffer.toString();
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}

	public static void main(String[] args) {

		String jaString = new String("\u65e5\u672c\u8a9e\u6587\u5b57\u5217");

		writeOutput(jaString);
		String inputString = readInput();
		String displayString = jaString + " " + inputString;
		new ShowString(displayString, "");
	}

}

class ShowString extends Frame {

	FontMetrics fontM;
	String outString;

	ShowString(String target, String title) {

		setTitle(title);
		outString = target;

		Font font = new Font("Monospaced", Font.PLAIN, 36);
		fontM = getFontMetrics(font);
		setFont(font);

		int size = 0;
		for (int i = 0; i < outString.length(); i++) {
			size += fontM.charWidth(outString.charAt(i));
		}
		size += 24;

		setSize(size, fontM.getHeight() + 60);
		setLocation(getSize().width / 2, getSize().height / 2);
		setVisible(true);
	}

	public void paint(Graphics g) {
		Insets insets = getInsets();
		int x = insets.left;
		int y = insets.top;
		g.drawString(outString, x + 6, y + fontM.getAscent() + 14);
	}
}
